package sw6.admin;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.SAXException;

import sw6.admin.gui.*;
import sw6.admin.gui.logic.*;
import sw6.admin.gui.logic.Menu;
import sw6.admin.parser.SettingsParser;
import sw6.lib.internal.PrivateDefinitions;
import android.app.*;
import android.content.*;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends ListActivity {

	ArrayList<AppEntry> mAppEntries;
	AppAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mAppEntries = new ArrayList<AppEntry>();
		mAdapter = new AppAdapter(this, R.layout.app_entry, mAppEntries);
		setListAdapter(mAdapter);

		loadApps();

		//loadSettingsForApp();
	}

	private void loadApps() {
		PackageManager pm = getPackageManager();
		List<ApplicationInfo> installedApplications = pm.getInstalledApplications(0);

		for (ApplicationInfo app : installedApplications) {
			if (app.packageName.startsWith(PrivateDefinitions.GIRAF_APP_PREFIX) == false)
				continue;
			try {
				if (SettingsParser.doWeHaveSettingsFile(MainActivity.this, app.packageName) == false)
					continue;
			} catch (NameNotFoundException e) {
				continue;
			} catch (IOException e) {
				continue;
			}
			AppEntry entry = new AppEntry();
			entry.setRealName(app.loadLabel(pm).toString());
			entry.setIcon(pm.getApplicationIcon(app));
			entry.setPackageName(app.packageName.toString());

			mAdapter.add(entry);
		}
	}

	private void loadSettingsForApp(String packageName){

		AssetManager am = null;
		
		try {
			am = SettingsParser.getAssetsByAppName(MainActivity.this, packageName);
		} catch (NameNotFoundException e1) {
		}
		
		InputStream is;
		
		try {
			is = am.open("settings.xml");
			Menu menu = SettingParser.LoadXML(is);

			byte[] bytes = Serializer.serializeObject(menu);
			Intent intent = new Intent();

			intent.setClassName(this, "sw6.admin.gui.MenuActivity");
			intent.putExtra("menuBytes", bytes);

			startActivity(intent);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		loadSettingsForApp(mAppEntries.get(position).getPackageName());

		super.onListItemClick(l, v, position, id);
	}

	private class AppAdapter extends ArrayAdapter<AppEntry> {

		private ArrayList<AppEntry> items;

		public AppAdapter(Context context, int textViewResourceId, ArrayList<AppEntry> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.app_entry, null);

			AppEntry o = items.get(position);
			TextView tt = (TextView) v.findViewById(R.id.toptext);
			ImageView iv = (ImageView) v.findViewById(R.id.icon);
			iv.setImageDrawable(o.getIcon());
			tt.setText(o.getRealName());

			return v;
		}
	}
}